<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        display: flex;
        justify-content: space-around;
      }

      .box .box__button {
        width: 100px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        cursor: pointer;
        user-select: none;
        border: 1px solid #000;
      }

      .box .box__button.active {
        color: green;
        background-color: yellow;
      }

      .button {
      }

      .button.active {
        color: red;
      }
    </style>
  </head>
  <body>
    <!-- 
      当某一系列组件是同级时，且他们之间有一个显示隐藏的关系，为了少些代码，可以使用动态组件

      <component is="xxx"></component>

      xxx 是需要渲染组件的名字，这个 component 组件就会替换成 xxx 组件

     -->
    <div id="app">
      <h2>Tab 切换</h2>
      <div>
        <button
          class="button"
          :class="{ active: curTabId === item.id }"
          v-for="item in tabs"
          :key="item.id"
          @click="curTabId = item.id"
        >
          {{item.name}}
        </button>
      </div>
      <!-- <plan-a v-if="curTabId === '001'"></plan-a>
      <plan-b v-if="curTabId === '002'"></plan-b>
      <plan-c v-if="curTabId === '003'"></plan-c> -->

      <component :is="`plan-${curTabId}`"></component>
    </div>

    <script src="./js/vue.js"></script>
    <script>
      Vue.component("plan-001", {
        template: `
          <div>Tab A 的内容</div>
        `,
        beforeDestroy() {
          console.log("plan-a beforeDestroy");
        },
      });

      Vue.component("plan-002", {
        template: `
          <div>Tab B 的内容</div>
        `,
      });

      Vue.component("plan-003", {
        template: `
          <div>Tab C 的内容</div>
        `,
      });

      // 根组件
      new Vue({
        el: "#app",

        data() {
          return {
            curTabId: "001", // 当前高亮的Tab的Id
            tabs: [
              { id: "001", name: "Tab A" },
              { id: "002", name: "Tab B" },
              { id: "003", name: "Tab C" },
            ],
          };
        },
      });
    </script>
  </body>
</html>
